/*
 * 产品类别页面视图
 * @Author: ljb
 * @Date: 2018-03-27 17:52:07
 * @Last Modified by: ljb
 * @Last Modified time: 2018-09-04 10:57:57
 */
<style lang="less" rel="stylesheet/less" type="text/less" scoped>
    .v-admin-product-category-views {
        padding: 20px;
    }
</style>

<template>
	<div class="v-admin-product-category-views">
		<Tabs
			type="card"
			@on-click="changeTab">

			<TabPane
				:label="$t('common.create')"
				name="新增">

				<category-create
					ref="create"
					:goods-categories="goods_categories"
					:languages="languages"
					@post="postData"/>

			</TabPane>

			<TabPane
				:label="$t('common.check')"
				name="查看">

				<category-index
					:loading="table_loading"
					:goods-categories="goods_categories"
					:categories="product_categories.data"
					@edit="categoryDetail"
					@delete="deleteCategory"
					@sort="confirmSearch"/>

				<br>

				<!-- 分页组件 -->
				<Row
					type="flex"
					justify="center">
					<Page
						:total="product_categories.total"
						:current="product_categories.current_page"
						:page-size="query_info.per_page"
						placement="top"
						show-sizer
						show-total
						@on-change="pageChange"
						@on-page-size-change="perPageChange"/>
				</Row>

			</TabPane>
		</Tabs>

		<Modal
			v-model="show_modal"
			:title="$t('category.update_category')"
			class-name="vertical-middle-modal">

			<!-- 产品类别编辑组件 -->
			<category-edit
				v-if="show_modal"
				:goods-categories="goods_categories"
				:category-detail="category_detail"
        :languages="languages"
        @update="updateCategory"/>

			<div slot="footer"/>

		</Modal>

	</div>

</template>

<script>

import API_LIST from 'src/API_LIST';
import { mapMutations } from 'vuex';
import qs from 'qs';

import categoryCreate from './create';
import categoryIndex from './index';
import categoryEdit from './edit';
import searchForm from './search-form';

export default {

	name: 'AdminCategoryViews',

	components: {
		categoryCreate,

		categoryIndex,

		categoryEdit,

		searchForm,
	},
	data() {
		return {
			query_info: {
				page: 1,

				per_page: 20,

				name: '',
			}, // 搜索参数

			search_info: {
				page: 1,

				per_page: 20,
			}, // 分页信息

			table_loading: false, // 表格更新时的loading

			show_modal: false, // 弹框的控制

			category_detail: {}, // 产品类别详情

			product_categories: {
				data: [],
			}, // 产品类别列表

			goods_categories: [], // 产品类别种类

      languages: [] // 多语言
    };
	},
	mounted() {
		this.fetchProductCategory();
	},
	methods: {

		...mapMutations('spinShow', [
			'showSpin',
			'closeSpin',
		]),

		/**
         * 切换tab
         * @param  {string} tab_name {tab的name}
         * @return {undefined}
         */
		changeTab(tab_name) {
			if (tab_name === '查看') {
				this.fetchProductCategoryList(this.query_info);
			} else {
				this.product_categories.data = [];
			}
		},

		/**
         * 页码改变
         * @param  {string} pageNumber {当前页码}
         * @return {undefined}
         */
		pageChange(pageNumber) {
			this.search_info.page = pageNumber;

			this.search_info.per_page = this.query_info.per_page;

			this.fetchProductCategoryList(this.search_info);
		},

		/**
         * 每页显示数改变
         * @param  {string} per_page {当前每页显示数}
         * @return {undefined}
         */
		perPageChange(per_page) {
			this.search_info.per_page = per_page;

			this.query_info.per_page = per_page;

			this.fetchProductCategoryList(this.query_info);
		},

		/**
         * 确认搜索
         * @param  {Object} param {搜索的参数对象}
         * @return {undefined}
         */
		confirmSearch({ name }) {
			this.query_info.name = name || '';

			this.search_info = JSON.parse(JSON.stringify(this.query_info));

			this.fetchProductCategoryList(this.query_info);
		},

		/**
     * 新增产品类别
		 * @param {Object} param {新增post数据}
     * @return {undefined}
     */
		postData(param) {
			this.showSpin(this.$t('common.please_wait'));

			this.$http.post(API_LIST.PRODUCT_CATEGORY, param)
				.then(({ data }) => {
					if (Number(data.code) !== 0) throw new Error(data.error);

					this.$Message.success(this.$t('common.create_success'));

					this.$refs.create.handleReset('formValidate');
				})
				.catch((err) => {
					this.$Message.error(err.message);
				})
				.then(this.closeSpin);
		},

		/**
         * 查看产品类别详情
         * @param  {string} id {合作伙伴的id}
         * @return {undefined}
         */
		categoryDetail(category) {
			this.category_detail = category;

			this.show_modal = true;
		},

		/**
         * 更新产品类别信息
         * @param  {Object} param {需要更新的参数对象}
         * @return {undefined}
         */
		updateCategory(id, param) {
			this.showSpin();

			this.$http.post(`${API_LIST.PRODUCT_CATEGORY}/${id}`, param)
				.then(({ data }) => {
					if (Number(data.code) !== 0) throw new Error(data.error);

					this.$Message.success(this.$t('common.update_success'));

					this.show_modal = false;

					this.fetchProductCategoryList(this.search_info);
				})
				.catch((err) => {
					this.$Message.error(err.message || err.statusText);
				})
				.then(this.closeSpin);
		},

		/**
         * 删除产品类别
         * @param  {string} id {需要删除的产品类别的id}
         * @return {undefined}
         */
		deleteCategory(id) {
			this.$Modal.confirm({

				title: this.$t('category.is_delete'), // '确定要删除此类别吗？',

				onOk: () => {
					const param = {
						_method: 'DELETE',
					};

					this.table_loading = true;

					this.$http.post(`${API_LIST.PRODUCT_CATEGORY}/${id}`, param)
						.then(({ data }) => {
							if (Number(data.code) !== 0) throw new Error(data.error);

							this.fetchProductCategoryList(this.search_info);

							this.$Message.success(this.$t('common.delete_success'));
						})
						.catch((err) => {
							this.$Message.error(err.message || err.statusText);
						})
						.then(() => { this.table_loading = false; });
				},

				onCancel: () => {},
			});
		},

		/**
     * 获取产品类别的分类
     * @return {undefined}
     */
		fetchProductCategory() {
			this.showSpin(this.$t('common.please_wait'));

			this.$http.get(`${API_LIST.PRODUCT_CATEGORY}/create`)
				.then(({ data }) => {
					if (Number(data.code) !== 0) throw new Error(data.error);

					this.goods_categories = data.goods_categories;

          // 多语言参数
          this.languages = data.languages;
				})
				.catch((err) => {
					this.$Message.error(err.message || err.statusText);
				})
				.then(this.closeSpin);
		},

		/**
         * 获取产品类别类别列表
         * @param  {Object} param {搜索列表的参数}
         * @return {undefined}
         */
		fetchProductCategoryList(param) {
			this.table_loading = true;

			const query_str = qs.stringify(param);

			this.$http.get(`${API_LIST.PRODUCT_CATEGORY}?${query_str}`)
				.then(({ data }) => {
					if (Number(data.code) !== 0) throw new Error(data.error);

					this.product_categories = data.categories;
				})
				.catch((err) => {
					this.$Message.error(err.message || err.statusText);
				})
				.then(() => { this.table_loading = false; });
		},
	},
};
</script>
